package lx300;

import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.OrientationRequested;
import modelo.ctrc;

public class MatrixMain {

    public static void print(String toPrint, String printerName) throws PrintException {  
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();  
        aset.add(OrientationRequested.PORTRAIT);  
        aset.add(new JobName("Impressao", null));  
           
        PrintService printer = null;  
        for (PrintService p : PrinterJob.lookupPrintServices()) {  
            if (p.getName().equalsIgnoreCase(printerName)) {  
                printer = p;  
                break;  
            }  
        }  
          
        DocPrintJob docPrint = printer.createPrintJob();             
        InputStream stream = new ByteArrayInputStream(toPrint.getBytes());  
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;  
        Doc doc = new SimpleDoc(stream, flavor, null);  
        docPrint.print(doc, aset);  
    }  


    public void imprimir(){
        ctrc c = new ctrc();
        c.setNumero(4444);
        c.setTipo("SAIDA");
        
       StringBuffer name = new StringBuffer("Marcio");
       name.append("Roloff");
        
//        EmissorLX300 e = new EmissorLX300(c, name);
//        e.emitir();
//        e.imprimir();
        
//        PrinterJob pj = PrinterJob.getPrinterJob();
//        if (pj.printDialog()) {
//        try {pj.print();}
//        catch (PrinterException exc) {
//            System.out.println(exc);
//         }
//     }  
        
    }
    public static void ImprimirCtrc(String codCtrc, String natOperacao, String codigo) {
        ESCPrinter escp = new ESCPrinter("\\\\IVAN\\EpsonLX-", false);
        //float a = (float) 1.3;
        if (escp.initialize()) {
            escp.setCharacterSet(ESCPrinter.BRAZIL);
            escp.select15CPI(); //15 characters per inch printing
            

            escp.advanceVertical(0.6f);
            escp.setAbsoluteHorizontalPosition(15);
            escp.bold(true);
            escp.print(codCtrc);

            escp.advanceVertical(0.9f); //go down 5cm foi6
            escp.setAbsoluteHorizontalPosition(14); //5cm to the right foi7
            escp.print(natOperacao);
            
            escp.advanceVertical(1.1f);
            escp.setAbsoluteHorizontalPosition(13); //back to 5cm on horizontal axis
            escp.print(codigo);
            
            escp.formFeed(); //eject paper
            escp.close(); //close stream            
        } else {
            System.out.println("Couldn't open stream to printer");
        }
    }

//    public static void main(String[] args) {
//        //ImprimirCtrc("5125","TRANSPORTE", "5352");
////       // ESCPrinter escp = new ESCPrinter("CutePDF Writer", false);
////        ESCPrinter escp = new ESCPrinter("\\\\IVAN\\EpsonLX-", false); //create ESCPrinter on network location \\computer\sharename, 9pin printer
////        if (escp.initialize()) {
////            escp.setCharacterSet(ESCPrinter.USA);
////            escp.select15CPI(); //15 characters per inch printing 
////            escp.advanceVertical(5); //go down 5cm foi6
////            escp.setAbsoluteHorizontalPosition(5); //5cm to the right foi7
////            escp.bold(true);
////            escp.print("Let's print some matrix text ;)");
////            escp.bold(false);
////            escp.advanceVertical(1);
////            escp.setAbsoluteHorizontalPosition(5); //back to 5cm on horizontal axis
////            escp.print("Very simple and easy!");            
////            escp.formFeed(); //eject paper
////            escp.close(); //close stream
////        }
////        else 
////            System.out.println("Couldn't open stream to printer");
//        //EmissorCtrc am = new EmissorCtrc(ctrc Ctrc, null);
////
////            @Override
////            public void emitir() {
////                throw new UnsupportedOperationException("Not supported yet.");
////            }
////        }
//        ctrc c = new ctrc();
//        c.setNumero(4444);
//        c.setTipo("SAIDA");
//        
//       StringBuffer name = new StringBuffer("Marcio");
//       name.append("Roloff");
//        
//        EmissorLX300 e = new EmissorLX300(c, name);
//        e.emitir();
//        e.imprimir();
//        
//    
//    }
}
